package com.zdpx.pxframework.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zdpx.pxframework.core.exception.GlobalException;
import com.zdpx.pxframework.redis.service.RedisService;
import com.zdpx.pxframework.system.api.entity.RoleInfo;
import com.zdpx.pxframework.system.api.enums.DataPermissionEnum;
import com.zdpx.pxframework.system.mapper.RoleInfoMapper;
import com.zdpx.pxframework.system.service.RoleInfoService;
import com.zdpx.pxframework.system.vo.codemsg.SystemCodeMsg;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import static com.zdpx.pxframework.core.constant.RedisKeyConstant.ROLE_SELF_DEPT_DATA_PERMISSION_KEY;


/**
 * 角色信息 服务实现类
 *
 * @Description:
 * @Author: Cheng XiaoXiao
 * @Date: 2022-02-27 15:20:14
 */
@Service
public class RoleInfoServiceImpl extends ServiceImpl<RoleInfoMapper, RoleInfo> implements RoleInfoService {
    @Resource
    private RedisService redisService;

    /**
     * 获取角色详情
     *
     * @param id ID
     * @return 单个角色详情
     */
    @Override
    public RoleInfo detailById(String id) {
        return this.getById(id);
    }

    /**
     * 增加角色信息
     *
     * @param roleInfo 角色对象
     */
    @Override
    public void saveRoleInfo(RoleInfo roleInfo) {
        //判断角色标识是否已经存在
        RoleInfo oldInfo = this.getByRoleKey(roleInfo.getRoleKey());
        if (oldInfo != null) {
            throw new GlobalException(SystemCodeMsg.ROLE_KEY_EXIST);
        }

        this.baseMapper.insert(roleInfo);
    }

    /**
     * 修改角色信息
     *
     * @param roleInfo 角色对象
     */
    @Override
    public void updateRoleInfoById(RoleInfo roleInfo) {
        //判断角色标识是否已经存在
        RoleInfo oldInfo = this.getByRoleKey(roleInfo.getRoleKey());
        if (oldInfo != null && !oldInfo.getId().equals(roleInfo.getId())) {
            throw new GlobalException(SystemCodeMsg.ROLE_KEY_EXIST);
        }
        //删除自定义的部门权限信息
        if (DataPermissionEnum.SELF.getScope().equals(roleInfo.getDataScope())) {
            String redisKey = ROLE_SELF_DEPT_DATA_PERMISSION_KEY + roleInfo.getId();
            redisService.deleteObject(redisKey);
        }

        this.baseMapper.updateById(roleInfo);
    }

    /**
     * 根据ID删除
     *
     * @param id ID
     */
    @Override
    public void deleteById(String id) {
        this.removeById(id);
    }

    /**
     * 根据角色标识获取角色信息
     *
     * @param roleKey 角色标识
     * @return 角色信息
     */
    @Override
    public RoleInfo getByRoleKey(String roleKey) {
        return this.getOne(new LambdaQueryWrapper<RoleInfo>().eq(RoleInfo::getRoleKey, roleKey));
    }

    /**
     * 根据用户ID获取拥有的角色标识列表
     *
     * @param userId 用户ID
     * @return 角色标识列表
     */
    @Override
    public Set<String> getRoleKeysByUserId(String userId) {
        return this.baseMapper.getRoleKeysByUserId(userId);
    }

    /**
     * 根据角色标识数组，获取角色列表
     *
     * @param roleKeys 角色标识数组
     * @return 角色列表
     */
    @Override
    public List<RoleInfo> listRoleInfoByRoleKeys(List<String> roleKeys) {
        if (roleKeys.isEmpty()) {
            return new ArrayList<>();
        }
        return this.baseMapper.selectList(new LambdaQueryWrapper<RoleInfo>().in(RoleInfo::getRoleKey, roleKeys));
    }
}
